package com.huike.sideslip;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 侧滑demo
 */
public class MainActivity extends AppCompatActivity {


    private ListView lv_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_main = findViewById(R.id.lv_main);

        lv_main.setAdapter(new ViewList());


    }

    class ViewList extends BaseAdapter {

        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        private SideSlipLayout slipLayout;
        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(MainActivity.this, R.layout.item_activity, null);
                holder = new ViewHolder(view);
                view.setTag(holder);

            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.tv_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this, "收藏" + i, Toast.LENGTH_SHORT).show();
                }
            });
            holder.tv_item2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this, "删除" + i, Toast.LENGTH_SHORT).show();
                }
            });

            SideSlipLayout layout = (SideSlipLayout) view;
            //只能打开一个
            layout.setOnStateChangeLisener(new SideSlipLayout.onStateChangeLisener() {
                @Override
                public void onDow(SideSlipLayout layout) {
                    if(slipLayout != null && slipLayout != layout){//判断打开不是当前侧滑是关闭 关闭
                        slipLayout.closeItem();
                    }
                }

                @Override
                public void onClose(SideSlipLayout layout) {
                    if(slipLayout != null){
                        slipLayout = null;
                    }
                }

                @Override
                public void onOpen(SideSlipLayout layout) {
                    slipLayout = layout;
                }
            });


            return view;
        }


    }

    class ViewHolder {
        public View rootView;
        public TextView tv_la;
        public TextView tv_item;
        public TextView tv_item2;


        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_la = (TextView) rootView.findViewById(R.id.tv_la);
            this.tv_item = (TextView) rootView.findViewById(R.id.tv_item);
            this.tv_item2 = (TextView) rootView.findViewById(R.id.tv_item2);
        }

    }


}
